package org.rpgmapper.database;

import java.util.ArrayList;
import java.util.List;

import org.rpgmapper.bo.Character;
import org.rpgmapper.bo.Hero;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import org.rpgmapper.bo.Foe;
import org.rpgmapper.bo.Stat;


public class RPGMapperOpenHelper extends SQLiteOpenHelper {

    private SQLiteDatabase db;
    private Context context;
    private final String[] columns = {DBConstants.ID_COL_NAME, DBConstants.TYPE_COL_NAME, DBConstants.NAME_COL_NAME, DBConstants.SIDE_COL_NAME,
			DBConstants.MAX_HITPOINTS_COL_NAME, DBConstants.MAX_MOVEPOINTS_COL_NAME, 
			DBConstants.HITPOINTS_COL_NAME, DBConstants.MOVEPOINTS_COL_NAME, DBConstants.AC_COL_NAME, DBConstants.INITIATIVE_COL_NAME, DBConstants.MONEY_COL_NAME};

    
    public void setDb(SQLiteDatabase db) {
		this.db = db;
	}

	public SQLiteDatabase getDb() {
		return db;
	}

	public RPGMapperOpenHelper(Context context) {
        super(context, DBConstants.DATABASE_NAME, null, DBConstants.DATABASE_VERSION);
        this.context = context;
    }

    @Override
    public void onCreate(SQLiteDatabase db) {
        db.execSQL(DBConstants.BONUS_TABLE_DROP);
        db.execSQL(DBConstants.CHARACTER_TABLE_DROP);

        db.execSQL(DBConstants.CHARACTER_TABLE_CREATE);
        db.execSQL(DBConstants.BONUS_TABLE_CREATE);

        db.execSQL(DBConstants.AYRIA_INSERT);
        db.execSQL(DBConstants.FOA_INSERT);
        db.execSQL(DBConstants.CALKIN_INSERT);
        db.execSQL(DBConstants.KEEMUN_INSERT);
        
        db.execSQL(DBConstants.ELF_INSERT);
        db.execSQL(DBConstants.GIANT_INSERT);
        db.execSQL(DBConstants.GOBLIN_INSERT);
        db.execSQL(DBConstants.GOD_INSERT);
        db.execSQL(DBConstants.WARRIOR_INSERT);
        db.execSQL(DBConstants.FLY_INSERT);
        db.execSQL(DBConstants.TROLL_INSERT);
        db.execSQL(DBConstants.PIXIE_INSERT);
        db.execSQL(DBConstants.HALF_ORC_INSERT);
    }
    
	@Override
	public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
	}

/**
 *
 * CHARACTERS
 * 
 */
	
	public void deleteAllCharacters() {
		db = getWritableDatabase();
		db.execSQL(DBConstants.BONUS_TABLE_DELETE_ALL);
		db.execSQL(DBConstants.CHARACTER_TABLE_DELETE_ALL);
		db.close();
	}

	public boolean insertCharacter(Character character) {
		db = getWritableDatabase();
		int type = (character instanceof Hero)?1:2;
		ContentValues args = new ContentValues();
        args.put(DBConstants.NAME_COL_NAME, character.getName());
        args.put(DBConstants.TYPE_COL_NAME, type);
        args.put(DBConstants.SIDE_COL_NAME, character.getSide());
        args.put(DBConstants.HITPOINTS_COL_NAME, character.get(Stat.HitPoints));
        args.put(DBConstants.MAX_HITPOINTS_COL_NAME, character.get(Stat.MaxHitPoints));
        args.put(DBConstants.MAX_MOVEPOINTS_COL_NAME, character.get(Stat.MovePoints));
        args.put(DBConstants.MOVEPOINTS_COL_NAME, character.get(Stat.MovePoints));
        args.put(DBConstants.INITIATIVE_COL_NAME, character.get(Stat.Init));
        args.put(DBConstants.AC_COL_NAME, character.get(Stat.AC));
        args.put(DBConstants.MONEY_COL_NAME, character.get(Stat.Money));
		db.insert(DBConstants.CHARACTER_TABLE_NAME, null, args);
		
		db.close();

		return true;
	}

	public boolean insertHero(String name, int side) {
		db = getWritableDatabase();
		
		System.err.println("on insert " + name);
		ContentValues args = new ContentValues();
        args.put(DBConstants.NAME_COL_NAME, name);
        args.put(DBConstants.TYPE_COL_NAME, 1);
        args.put(DBConstants.SIDE_COL_NAME, side);
        args.put(DBConstants.HITPOINTS_COL_NAME, 20);
        args.put(DBConstants.MAX_HITPOINTS_COL_NAME, 20);
        args.put(DBConstants.MAX_MOVEPOINTS_COL_NAME, 6);
        args.put(DBConstants.MOVEPOINTS_COL_NAME, 6);
        args.put(DBConstants.INITIATIVE_COL_NAME, 12);
        args.put(DBConstants.AC_COL_NAME, 15);
        args.put(DBConstants.MONEY_COL_NAME, 0);
		db.insert(DBConstants.CHARACTER_TABLE_NAME, null, args);
		
		db.close();
		
		return true;
	}
	
	public boolean insertMonster(String name) {
		db = getWritableDatabase();

		ContentValues args = new ContentValues();
        args.put(DBConstants.NAME_COL_NAME, name);
        args.put(DBConstants.TYPE_COL_NAME, 2);
        args.put(DBConstants.SIDE_COL_NAME, 0);
        args.put(DBConstants.HITPOINTS_COL_NAME, 20);
        args.put(DBConstants.MAX_HITPOINTS_COL_NAME, 20);
        args.put(DBConstants.MAX_MOVEPOINTS_COL_NAME, 6);
        args.put(DBConstants.MOVEPOINTS_COL_NAME, 6);
        args.put(DBConstants.INITIATIVE_COL_NAME, 12);
        args.put(DBConstants.AC_COL_NAME, 15);
        args.put(DBConstants.MONEY_COL_NAME, 0);
		db.insert(DBConstants.CHARACTER_TABLE_NAME, null, args);

		db.close();
		
		return true;
	}
	
	public void updateCharacter(Character c) {
		db = getWritableDatabase();
		System.err.println("on update " + c.toString());
		ContentValues args = new ContentValues();
        args.put(DBConstants.ID_COL_NAME, c.getId());
        args.put(DBConstants.NAME_COL_NAME, c.getName());
        args.put(DBConstants.TYPE_COL_NAME, (c instanceof Hero)?1:2);
        args.put(DBConstants.SIDE_COL_NAME, c.getSide());
        args.put(DBConstants.HITPOINTS_COL_NAME, c.get(Stat.HitPoints));
        args.put(DBConstants.MAX_HITPOINTS_COL_NAME, c.get(Stat.HitPoints));
        args.put(DBConstants.MAX_MOVEPOINTS_COL_NAME, c.get(Stat.MovePoints));
        args.put(DBConstants.MOVEPOINTS_COL_NAME, c.get(Stat.MovePoints));
        args.put(DBConstants.INITIATIVE_COL_NAME, c.get(Stat.Init));
        args.put(DBConstants.AC_COL_NAME, c.get(Stat.AC));
        args.put(DBConstants.MONEY_COL_NAME, c.get(Stat.Money));
        db.update(DBConstants.CHARACTER_TABLE_NAME, args, DBConstants.ID_COL_NAME+"="+c.getId(), null);
        db.close();
	}

	public List<Character> getAllCharacters() {
		List<Character> chars = new ArrayList<Character>();
		db = getReadableDatabase();
		Cursor cur = db.query(DBConstants.CHARACTER_TABLE_NAME, columns, "", null, "", "", "");
		int i = 0;
		while (cur.moveToNext() != false) {
			Character c;
			if (cur.getInt(1) == 1) {
				c = new Hero();
			} else {
				c = new Foe();
			}
			c.setId(cur.getInt(0));
			c.setName(cur.getString(2));
			c.setSide(cur.getInt(3));
			c.setBase(Stat.MaxHitPoints,cur.getInt(4));
			c.setBase(Stat.MovePoints,cur.getInt(5));
			c.setBase(Stat.HitPoints,cur.getInt(6));
			c.setBase(Stat.MovePoints,cur.getInt(7));
			c.setBase(Stat.AC,cur.getInt(8));
			c.setBase(Stat.Init,cur.getInt(9));
			c.setBase(Stat.Money,cur.getInt(10));
			chars.add(c);
			i++;
		}
		cur.close();
		db.close();
		return chars;
	}

	public List<Character> getAllHeroes() {
		List<Character> chars = new ArrayList<Character>();
		db = getReadableDatabase();
		Cursor cur = db.query(DBConstants.CHARACTER_TABLE_NAME, columns, DBConstants.TYPE_COL_NAME+"='1'", null, "", "", "");
		int i = 0;
		while (cur.moveToNext() != false) {
			Character c;
			c = new Hero();
			c.setId(cur.getInt(0));
			c.setName(cur.getString(2));
			c.setSide(cur.getInt(3));
			c.setBase(Stat.MaxHitPoints,cur.getInt(4));
			c.setBase(Stat.MovePoints,cur.getInt(5));
			c.setBase(Stat.HitPoints,cur.getInt(6));
			c.setBase(Stat.MovePoints,cur.getInt(7));
			c.setBase(Stat.AC,cur.getInt(8));
			c.setBase(Stat.Init,cur.getInt(9));
			c.setBase(Stat.Money,cur.getInt(10));
			chars.add(c);
			i++;
		}
		cur.close();
		db.close();
		return chars;
	}

	public List<Character> getAllMonsters() {
		List<Character> chars = new ArrayList<Character>();
		db = getReadableDatabase();
		Cursor cur = db.query(DBConstants.CHARACTER_TABLE_NAME, columns, DBConstants.TYPE_COL_NAME+"='2'", null, "", "", "");
		int i = 0;
		while (cur.moveToNext() != false) {
			Character c;
			c = new Foe();
			c.setId(cur.getInt(0));
			c.setName(cur.getString(2));
			c.setSide(cur.getInt(3));
			c.setBase(Stat.MaxHitPoints,cur.getInt(4));
			c.setBase(Stat.MovePoints,cur.getInt(5));
			c.setBase(Stat.HitPoints,cur.getInt(6));
			c.setBase(Stat.MovePoints,cur.getInt(7));
			c.setBase(Stat.AC,cur.getInt(8));
			c.setBase(Stat.Init,cur.getInt(9));
			c.setBase(Stat.Money,cur.getInt(10));
			chars.add(c);
			i++;
		}
		cur.close();
		db.close();
		return chars;
	}

	public Character getCharacterById(long id) {
		db = getReadableDatabase();
		Character c = null;
		Cursor cur = db.query(DBConstants.CHARACTER_TABLE_NAME, columns, DBConstants.ID_COL_NAME+"='"+id+"'", null, "", "", "");
		if (cur.getCount() > 0) {
			cur.moveToNext();
			if (cur.getInt(1) == 1) {
				c = new Hero();
			} else {
				c = new Foe();
			}
			c.setId(cur.getInt(0));
			c.setName(cur.getString(2));
			c.setSide(cur.getInt(3));
			c.setBase(Stat.MaxHitPoints,cur.getInt(4));
			c.setBase(Stat.MovePoints,cur.getInt(5));
			c.setBase(Stat.HitPoints,cur.getInt(6));
			c.setBase(Stat.MovePoints,cur.getInt(7));
			c.setBase(Stat.AC,cur.getInt(8));
			c.setBase(Stat.Init,cur.getInt(9));
			c.setBase(Stat.Money,cur.getInt(10));
		}
		cur.close();
		db.close();
		return c;
	}
	
	public void deleteCharacter(long id) {
		db = getWritableDatabase();
		db.delete(DBConstants.CHARACTER_TABLE_NAME, DBConstants.ID_COL_NAME+"="+id, null);
		db.close();
	}
}
